/**
 * Created by LeungZ on 2016/8/8.
 */
class Overview {
    constructor(ctgNotification, dp, node , $timeout, nodeSrv, $scope, $stateParams, detail, nodeResource,lessee) {
        "ngInject";
        this._notification = ctgNotification;
        this._timeout = $timeout;
        this._nodeSrv = nodeSrv;
        this.timer = '';
        this.dbCode = $stateParams.kDbCode;

        if(detail){
            this.dbName = detail[0].clusterName;
        }
    

        // if (cluster) {
        //     this.node = cluster.nodeNum;
        //     this.pod = cluster.podsNum;
        //     this.service = cluster.rcNum;
        //     // this.lessee=cluster.lesNum;
        // }
        this.node=node.num;
        this.service=dp.listMeta.totalItems;
        this.lessee=lessee.total;
        if (node) {
            this.resData = [];
            this.nodeIps = [];

            angular.forEach(node.nodes, (_node, index)=> {
                _node.dynamicStatus = nodeResource[index];
                this.nodeIps.push(_node.nodeIp);
                this.setNodesResource(_node, index);
                _node.nodeStatus = _node.conditions.status == 'True';
            });

            this.nodes = node.nodes;
            this.refreshNodesResource();
        }

        this.config = {
            type: 'pie',
            showLegend: false,
            tooltip: {
                trigger: 'item',
                formatter: function (params) {
                    if (params.seriesName == 'CPU') {
                        return 'CPU ' + params.name + ' ' + params.value + '%'
                    }
                    if (params.seriesName == 'MEM') {
                        return params.name + ' ' + params.value + 'MB'
                    }
                }
            },
            series: [
                {
                    radius: ['55%', '65%'],
                    label: {
                        normal: {
                            show: false
                        }
                    },
                    labelLine: {
                        normal: {
                            show: false
                        }
                    }
                },
                {
                    radius: ['80%', '90%'],
                    label: {
                        normal: {
                            show: false
                        }
                    },
                    labelLine: {
                        normal: {
                            show: false
                        }
                    }
                }
            ],
            color: ['#50c14e', '#ccc', '#5093e1', '#ccc']
        };

        $scope.$on('$destroy', ()=> {
            this._timeout.cancel(this.timer);
        })
    }

    refreshNodesResource() {
        this.timer = this._timeout(this.getNodesRes.bind(this), 10000);
    }

    getNodesRes() {
        this._nodeSrv.queryNodeResource({nodeips: this.nodeIps, clusterCode: this.dbCode}).then(data=> {
            angular.forEach(data, (node, index)=> {
                this.nodes[index].nodeStatus = node.ready;
                node.dynamicStatus = data[index];
                this.setNodesResource(node, index);
            });
            this.refreshNodesResource();
        }).catch(data=> {
            this._notification.error('获取宿主机资源数据有误.');
        });
    }

    setNodesResource(node, index) {
        this.resData[index] = [{
            name: 'CPU',
            datapoints: [
                {x: '已使用', y: node.dynamicStatus.totalCpuUsage},
                {x: '未使用', y: 100 - node.dynamicStatus.totalCpuUsage}
            ]
        }, {
            name: 'MEM',
            datapoints: [
                {x: '占用内存', y: node.dynamicStatus.memoryUsage},
                {x: '空闲内存', y: node.dynamicStatus.totalMemory - node.dynamicStatus.memoryUsage}
            ],
            memText: node.dynamicStatus.memoryUsage > 9999 ? (node.dynamicStatus.memoryUsage / 1024).toFixed(0) + 'GB'
                : node.dynamicStatus.memoryUsage + 'MB'
        }];
    }
}

export default Overview;